/**
 *
 */
package co.fitstart.api.web.controller.content;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.api.service.ContentService;
import co.fitstart.entity.content.Article;
import co.fitstart.entity.content.page.Webpage;

/**
 * @author downpour
 *
 */
@Controller
@RequestMapping("/v1/web-pages")
public class WebPageApiController {

    @Autowired
    private ContentService contentService;

    /**
     * Map by alias
     *
     * @param alias
     * @return
     */
    @RequestMapping("/{alias}")
    public @ResponseBody String load(@PathVariable("alias") String alias) {

        Result result = new Result();

        Webpage webpage = contentService.getWebPage(alias);
        if(webpage != null) {
            result.success()
                  .data(CollectionUtils.constructMapValuesFromList(webpage.getLinks(), "alias"))
                  .data(CollectionUtils.constructMapValuesFromList(webpage.getBriefs(), "alias"));

            Map<String, List<Article>> articles = contentService.listArticle(webpage);
            if(articles != null) {
                result.data(articles);
            }
        } else {
            result.fail().message("获取后台数据失败！");
        }
        return JsonUtils.toJsonString(result);
    }

}
